import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:package_info_plus/package_info_plus.dart';

import '../../constants.dart';
import '../../route/route_utils.dart';
import '../../route/routes.dart';

class AboutOurPage extends StatefulWidget {
  const AboutOurPage({super.key});

  @override
  State<AboutOurPage> createState() => _AboutOurPageState();
}

class _AboutOurPageState extends State<AboutOurPage> {
  String _version = '';

  @override
  void initState() {
    super.initState();
    _getAppVersion();
  }

  Future<void> _getAppVersion() async {
    final PackageInfo packageInfo = await PackageInfo.fromPlatform();
    setState(() {
      _version = 'V${packageInfo.version}';
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('关于软件'),
        leading: IconButton(
          icon: const Icon(Icons.arrow_back),
          onPressed: () => Navigator.of(context).pop(),
        ),
      ),
      backgroundColor: const Color.fromRGBO(244, 247, 250, 1),
      body: Column(
        children: [
          // Logo和版本信息区域
          Container(
            width: double.infinity,
            padding: const EdgeInsets.symmetric(vertical: 40),
            color: Colors.white,
            child: Column(
              children: [
                // 应用Logo - 替换为你的应用Logo
                Container(
                  width: 80,
                  height: 80,
                  decoration: BoxDecoration(
                    color: Theme.of(context).colorScheme.primary,
                    borderRadius: BorderRadius.circular(16),
                  ),
                  child: Image.asset(
                    'assets/images/logo.png',
                    width: 40.r,
                    height: 40.r,
                    fit: BoxFit.cover,
                  ),
                ),
                const SizedBox(height: 20),
                // 应用名称
                const Text(
                  '赏金侠',
                  style: TextStyle(
                    fontSize: 18,
                    fontWeight: FontWeight.bold,
                    color: Color(0xFF262626),
                  ),
                ),
                const SizedBox(height: 8),
                // 版本号
                Text(
                  _version,
                  style: const TextStyle(
                    fontSize: 14,
                    color: Color(0xFF8C8C8C),
                  ),
                ),
              ],
            ),
          ),
          const SizedBox(height: 12),

          // 功能列表
          _buildListItem(
            '用户协议',
            onTap: () {
              RouteUtils.pushForNamed(context, RoutePath.web_view, arguments: {'url': Constants.Server_Protocol});
            },
          ),
          const Divider(height: 1, indent: 15),
          _buildListItem(
            '隐私协议',
            onTap: () {
              RouteUtils.pushForNamed(context, RoutePath.web_view, arguments: {'url': Constants.Privacy_Protocol});
            },
          ),
          // const Divider(height: 1, indent: 15),
          // _buildListItem(
          //   '检查更新',
          //   onTap: _checkForUpdate,
          // ),
          const SizedBox(height: 12),
          _buildListItem(
            '客服中心',
            onTap: _contactCustomerService,
          ),
          const Divider(height: 1, indent: 15),
          _buildListItem(
            '意见反馈',
            onTap: _submitFeedback,
          ),

          // 底部版权信息
          const Expanded(
            child: Align(
              alignment: Alignment.bottomCenter,
              child: Padding(
                padding: EdgeInsets.only(bottom: 40),
                child: Text(
                  'Copyright © 2017-至今 苏州霸王融网络科技有限公司\nAll Rights Reserved',
                  textAlign: TextAlign.center,
                  style: TextStyle(
                    fontSize: 12,
                    color: Color(0xFF8C8C8C),
                  ),
                ),
              ),
            ),
          ),
        ],
      ),
    );
  }

  Widget _buildListItem(String title, {VoidCallback? onTap}) {
    return Material(
      color: Colors.white,
      child: InkWell(
        onTap: onTap,
        child: Container(
          padding: const EdgeInsets.symmetric(horizontal: 15, vertical: 15),
          child: Row(
            children: [
              Expanded(
                child: Text(
                  title,
                  style: const TextStyle(
                    fontSize: 16,
                    color: Color(0xFF262626),
                  ),
                ),
              ),
              const Icon(
                Icons.arrow_forward_ios,
                size: 16,
                color: Color(0xFF8C8C8C),
              ),
            ],
          ),
        ),
      ),
    );
  }

  void _checkForUpdate() {
    // 检查更新逻辑
    showDialog(
      context: context,
      builder: (BuildContext context) {
        return AlertDialog(
          title: const Text('提示'),
          content: const Text('当前已是最新版本'),
          actions: [
            TextButton(
              child: const Text('确定'),
              onPressed: () => Navigator.of(context).pop(),
            ),
          ],
        );
      },
    );
  }

  void _contactCustomerService() {
    // 联系客服
    showDialog(
      context: context,
      builder: (BuildContext context) {
        return AlertDialog(
          title: const Text('提示'),
          content: const Text('客服微信号：BWR_Ltd'),
          actions: [
            TextButton(
              child: const Text('取消'),
              onPressed: () => Navigator.of(context).pop(),
            ),
            TextButton(
              child: const Text('复制'),
              onPressed: () {
                Navigator.of(context).pop();
                Clipboard.setData(ClipboardData(text: 'BWR_Ltd'));
              },
            ),
          ],
        );
      },
    );
  }

  void _submitFeedback() {
    // 意见反馈
    RouteUtils.pushForNamed(context, RoutePath.feedback);
  }
}